
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QClipboard>
#include <QPixmap>
#include <httpclient.h>
#include <qrcutil.h>

#define FONT_ID 991
#define FONT_TEXT 992
#define FONT_TYPE 993

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
  , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    QStringList fonts = QR("://fonts.txt").split("\n");
    foreach (auto var, fonts) {
        auto parts = var.split(","); // 165,新蒂黑板,xdhb.ttf
        if (parts.length() != 3) {
            continue;
        }

        QListWidgetItem *item = new QListWidgetItem;
        item->setData(FONT_ID, parts.at(0));
        item->setData(FONT_TEXT, parts.at(1));
        item->setData(FONT_TYPE, parts.at(2));
        item->setText(parts.at(1));
        ui->wa_font_list->addItem(item);
    }
}
MainWindow::~MainWindow()
{
	
}

void MainWindow::on_wa_font_list_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    if (!current) return;

    QString type = current->data(FONT_TYPE).toString();

    Request req("https://www.yishuzi.cn/image.png");
    req.addQuery("fsize", "30");
    req.addQuery("font", type);
    req.addQuery("text", ui->wa_text_input->text());
    req.addQuery("mirror", "no");
    req.addQuery("color", "111");
    req.addQuery("vcolor", "111");
    req.addQuery("bgcolor", "fff");
    req.addQuery("alpha", "no");
    req.addQuery("output", "png");
    req.addQuery("spacing", "4");
    req.addQuery("shadow", "no");
    req.addQuery("transparent", "no");
    req.addQuery("icon", "no");
    req.addQuery("iconic", "");
    req.addQuery("top_spacing", "5");
    req.addQuery("left_spacing", "6");
    req.addQuery("icon_size", "48");

    QImage image = QImage::fromData(HttpClient::instance().get(req).content());
    image.isNull();


    QPixmap pixmap = QPixmap::fromImage(image);
//    if
    QSize size = ui->label->rect().size();
    if (size.width() < pixmap.width()) {
        pixmap = pixmap.scaledToHeight(size.height());
    } else if (size.height() < pixmap.height()) {
        pixmap = pixmap.scaledToWidth(pixmap.width());
    }
    ui->label->setPixmap(pixmap);
}


/***
 * 'https://www.yishuzi.cn/image.png
 *  ?fsize=30
 *  &font=ygxk.ttf
 *  &text=%E6%98%9F%E7%81%AB
 *  &mirror=no
 *  &color=111
 *  &vcolor=111
 *  &bgcolor=fff
 *  &alpha=no
 *  &output=png
 *  &spacing=4
 *  &shadow=no
 *  &transparent=no
 *  &icon=no
 *  &iconic=
 *  &top_spacing=5
 *  &left_spacing=6
 *  &icon_size=48'
 *
 */

void MainWindow::on_action_image_copy_triggered()
{
    const QPixmap *pixmap = ui->label->pixmap();
    if (!pixmap->isNull()) {
        qApp->clipboard()->setPixmap(*pixmap);
    }
}


void MainWindow::on_label_customContextMenuRequested(const QPoint &pos)
{
    QMenu menu;
    menu.addAction(ui->action_image_copy);
    menu.exec(QCursor::pos());
}

